package com.goalias.controller;

import com.goalias.exception.BizException;
import com.goalias.feign.AbilityClient;
import com.goalias.resp.Result;
import com.goalias.utils.IoUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/content")
public class TestController {

    @Resource
    private AbilityClient abilityClient;

    @Resource
    private IoUtil iOUtil;

    @GetMapping("/test")
    public String test() {
        String hello = abilityClient.hello(1);
        return "test"+hello;
    }

    @GetMapping("/getFile")
    public String test2() {
        String url = iOUtil.getUrl(iOUtil.getBUCKET_NAME(), "user/微信图片_20220201180517.jpg");
//        iOUtil.downloadFile("goalias-blog", "headPic.jpg", "E:\\MinIO\\test\\headPic.jpg");
        return url;
    }

    @PostMapping("/uploadFile")
    public Result test3(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();
        try {
            String fileName = iOUtil.uploadFile(iOUtil.getBUCKET_NAME(), iOUtil.USER_PATH()+name, file.getInputStream());
            return Result.success(fileName);
        } catch (IOException e) {
            throw new BizException("读取文件失败");
        }
    }
}
